//
//  PageViewController.m
//  片刻
//
//  Created by 黄俊杰 on 15/12/21.
//  Copyright © 2015年 黄俊杰. All rights reserved.
//

#import "PageViewController.h"
#import "LDJ_TopicCell.h"
#import "LDJ_LablelCell.h"
#import "LDJ_SoudCell.h"
#import "LDJ_StractionCell.h"
#import "LDJ_MusicCell.h"
#import "LDJ_WorldCell.h"
#import "LDJ_HomeDetailViewController.h"
#import "LDJ_PhotoViewController.h"
#import "HJJPlayerViewController.h"
#import "HJJRadioRootModel.h"


@interface PageViewController ()<LDJ_StractionCellDelegate>
@end

@implementation PageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self creacteTableView];
    
    
    
    
}

-(void)creacteTableView{

    self.bigArray = [NSMutableArray array];
    self.array = [NSMutableArray array];
    self.headerView = [[LDJ_PageHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 140 * EACH_HEIGHT)];
    
    self.myTableView = [[UITableView alloc] init];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.tableHeaderView = self.headerView;
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(0);
        make.left.bottom.right.mas_equalTo(0);
    }];
    
#pragma mark 注册
    /**<Topic 标题 图片 cell*/
    [self.myTableView registerClass:[LDJ_TopicCell class] forCellReuseIdentifier:@"topicCell"];
    /**<Label 没有标题的 cell*/
    [self.myTableView registerClass:[LDJ_LablelCell class] forCellReuseIdentifier:@"labelCell"];
    /**<Sound 大播放按钮的 cell*/
    [self.myTableView registerClass:[LDJ_SoudCell class] forCellReuseIdentifier:@"soundCell"];
    /**<插画 三图 cell*/
    [self.myTableView registerClass:[LDJ_StractionCell class] forCellReuseIdentifier:@"stractionCell"];
    /**<Music  cell*/
    [self.myTableView registerClass:[LDJ_MusicCell class] forCellReuseIdentifier:@"musicCell"];
    /**<World 图 标题 内容 cell*/
    [self.myTableView registerClass:[LDJ_WorldCell class] forCellReuseIdentifier:@"worldCell"];
    
    
    [self addHeader];
    [self addFooter];
}


-(void)addHeader{

    

    
    MJRefreshGifHeader *header = [WWY_AppTools headerWithRefreshingBlock:^{
        self.nextPage = 0;
        self.isUpLoading = NO;
        [self GetPersonList];
    }];
    self.myTableView.mj_header = header;
    [self.myTableView.mj_header beginRefreshing];
    
    
    
    
}

-(void)addFooter{
    
    


    MJRefreshBackGifFooter *footer = [WWY_AppTools footerWithRefreshingBlock:^{
        self.nextPage += 10;
        self.isUpLoading = YES;
        [self GetPersonList];
        
    }];
    
    self.myTableView.mj_footer = footer;

    
}


-(void)GetPersonList{

    
   
    NSString *urlStr = @"http://api2.pianke.me/pub/today";
    NSString *body =[NSString stringWithFormat: @"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&start=%ld&version=3.0.6", self.nextPage];
    
    [LDJ_AppTools postUrl:urlStr body:body result:LDJJSON requestStyle:LDJRequestString headerFile:nil success:^(id result) {
        
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"list"];
        if (self.isUpLoading == NO) {
            [self.bigArray removeAllObjects];
        }
        
        for (NSMutableDictionary *dic in array) {
            if ([[dic objectForKey:@"enname"] isEqualToString:@"Sound"]) {
            
                
                    HJJRadioRootModel *model = [[HJJRadioRootModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    model.musicUrl = [[dic objectForKey:@"playInfo"] objectForKey:@"musicUrl"];
                    model.webview_url = [[dic objectForKey:@"playInfo"] objectForKey:@"webview_url"];
                    model.uname = [[[dic objectForKey:@"playInfo"] objectForKey:@"userinfo"]objectForKey:@"uname"];
                
                    [self.array addObject:model];
                
            }
            
            LDJ_HomeModel *model = [[LDJ_HomeModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            //自适应内容的高度
            CGSize textHeight = [WWY_AppTools SizeForCommitSize:CGSizeMake(SCREEN_WIDTH - 20 *EACH_WIDTH, MAXFLOAT) andFont:[UIFont systemFontOfSize:16 * EACH_HEIGHT] andContent:model.content];
            model.height = textHeight.height;
            
            [self.bigArray addObject:model];
        }
        
        
        // 取出轮播图的图片数组
        self.headerView.showImgArray = [dic objectForKey:@"carousel"];
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];

        
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.bigArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LDJ_HomeModel *homeModel = [self.bigArray objectAtIndex:indexPath.row];
    
    if ([homeModel.enname isEqualToString:@"Sound"]) {
        

       LDJ_SoudCell *soundCell = [tableView dequeueReusableCellWithIdentifier:@"soundCell"];
        soundCell.soundModel = homeModel;
//        //去除点击cell时触发的效果
        soundCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return soundCell;

        
        
    }else if ([homeModel.name isEqualToString:@"碎片"]){
        
        LDJ_LablelCell *labelCell = [tableView dequeueReusableCellWithIdentifier:@"labelCell"];
        labelCell.labelModel = homeModel;
        labelCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return labelCell;

        
        
        
    }else if ([homeModel.name isEqualToString:@"插画"] || [homeModel.name isEqualToString:@"摄影"]){
        
        LDJ_StractionCell *illCell = [tableView dequeueReusableCellWithIdentifier:@"stractionCell"];
        illCell.stractionCellDelegate = self;
        illCell.selectionStyle = UITableViewCellSelectionStyleNone;
        illCell.illModel = homeModel;
        return illCell;

        
        
    }else if ([homeModel.name isEqualToString:@"话题"]){
        
        LDJ_TopicCell *topicCell = [tableView dequeueReusableCellWithIdentifier:@"topicCell"];
        topicCell.topicModel = homeModel;
        topicCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return topicCell;

    
        
    }else if ([homeModel.name isEqualToString:@"音乐"]){
    
       LDJ_MusicCell *musicCell = [tableView dequeueReusableCellWithIdentifier:@"musicCell"];
        musicCell.musicModel = homeModel;
        musicCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return musicCell;
    }else{
        
        LDJ_WorldCell *worldCell = [tableView dequeueReusableCellWithIdentifier:@"worldCell"];
        worldCell.worldModel = homeModel;
        worldCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return worldCell;
    }

    

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    LDJ_HomeDetailViewController *detailVC = [[LDJ_HomeDetailViewController alloc] init];
    LDJ_HomeModel *model = [self.bigArray objectAtIndex:indexPath.row];
    if ([model.enname isEqualToString:@"Sound"]) {
        
        
        [HJJPlayerViewController sharePlayerViewController].listArray = self.array;
        [self presentViewController:[HJJPlayerViewController sharePlayerViewController] animated:YES completion:^{
            
            
        }];
        
    }
    else{
    detailVC.detaId = model._id;
    detailVC.titleStr = model.title;
    [self.navigationController pushViewController:detailVC animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LDJ_HomeModel *model = [self.bigArray objectAtIndex:indexPath.row];
    
    if ([model.name isEqualToString:@"插画"] || [model.name isEqualToString:@"摄影"]) {
        return model.height + 500 * EACH_HEIGHT;
    }else if ([model.name isEqualToString:@"音乐"]){
    
        return model.height + 200 * EACH_HEIGHT;
    }else if ([model.name isEqualToString:@"碎片"]){
    
        return model.height + 300 * EACH_HEIGHT;
        
    }
    
    else{

    return model.height + 350 * EACH_HEIGHT;
    }
}

-(void)presentToViewController:(NSInteger)number ImgArray:(NSMutableArray *)imgArray{
    
    

    LDJ_PhotoViewController *photoVC = [[LDJ_PhotoViewController alloc]init];
    photoVC.photoArray = imgArray;
    photoVC.photoNumber = number - 1000;
    // 选择模态的风格
    [photoVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    
    [self.view.window.rootViewController presentViewController:photoVC animated:YES completion:^{
        
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
